var privileges ;
var canUpdate = false;
var canSet = false;


function clickSearch(){
	
  //查询用户的修改权限
	privileges = checkUserPrivilege(['2510','2520','2530','2540']);
	//用户权限判断
	if (privileges.result == "true") {
		var add="";
		var del="";
		var update="";
		var refresh='<a class="btn" href="javascript:location.reload()"><i class="icon-refresh"></i>刷新</a> ';
		var adds="";
		$(privileges.data).each(function(ix, ite) {
			$("#operator a").remove();
			if (ite.code=="2510" && ite.value=="1"){
				//新增
				add='<a class="btn" href="addRoomInfo.jsp"><i class="icon-plus"></i>新增房间信息</a>';
			}
			if (ite.code=="2520" && ite.value=="1"){
				//删除
				del = '<a class="btn" href="javascript:removeRoom()"><i class="icon-minus"></i>删除房间信息</a>  ';
			}
			if (ite.code=="2530" && ite.value=="1"){
				//修改
				canUpdate = true;
			}
			if (ite.code=="2540" && ite.value=="1"){
				//批量新增用户 --href="javascript:location.reload()"
				adds = '<a class="btn" data-toggle="modal"  data-target="#myModal1" ><i class="icon-plus"></i>批量新增</a>';
				$('#myModal1').on('show.bs.modal', function () {
					  // 执行一些动作...
					//初始化fileinput控件（第一次初始化）
		            initFileInput("file-Portrait1", "roomAction!uploadFile.action");
		            //initFileInput("file-Portrait1");
					});
			}
		});
		$("#operator").prepend(add+del+adds+refresh);
	}else {
		alert("查询失败"+data.msg);
	}
    
	var roomName = $("input[name=keywords]").val();
	var inParma={};
	inParma.roomName=roomName;
	inParma.qryType="all";
	$.post(
		"roomAction!getRoom.action",
		{inParma : JSON.stringify(inParma)},
		function(data){
			data = eval("("+data+")");
			if(data!=null&&data.result=="true"){
				$("#demo").myPagination({
				currPage: 1,
				pageSize: 5,
				pageCount:data.pageCount,
				ajax:{
					on: true,                        //开启状态
		            // url: "mbook!getMBookByCondition.action?type="+type+"&keyword="+keyword,            //访问服务器地址
					url: "roomAction!getRoom.action",
		            dataType: 'json',                //返回类型
		            callback:'callBack',
		            param:{"on":true, inParma : JSON.stringify(inParma)},
		            ajaxStart: function() {
		                return false;
		            },onClick:function(page){
		            	alert(page);
		            }
				}
			});
			}else{
				alert("查询失败:"+data.msg);
			}
			
		}
	);
}

function callBack(data){
	$("#listTable>tbody").children().remove();
	data = eval("("+data+")");
	$(data.data).each(function(ix,ite){
		var trStr = '<tr>';
			trStr += '<td class="with-checkbox"><input type="checkbox" name="check" value="'+ite.roomId+'" /></td>';
			//小区
			if (ite.community != null ){
				trStr += '<td>'+StrUtil.strnull(ite.community.communityName)+'</td>';
			}else{
				trStr += '<td></td>';
			}
			
			if(ite.building != null){
				var buildingName = StrUtil.strnull(ite.building.buildingName);
				if (ite.unit != null){
					buildingName += "-" + StrUtil.strnull(ite.unit.buildingName);
				}
				trStr += '<td>'+buildingName+'</td>';
			}else{
				trStr += '<td></td>';
			}
			/*if(ite.building != null && ite.building.buildingName!=null){
				trStr += '<td>'+ite.building.buildingName+'</td>';
			}else{
				trStr += '<td></td>';
			}*/
			trStr += '<td>'+StrUtil.strnull(ite.roomNbr)+'</td>';
			//trStr += '<td>'+strUtil.Strnull(ite.partyInfo.partyName)+'</td>';
			if(ite.ownerInfo != null ){
				trStr += '<td>'+StrUtil.strnull(ite.ownerInfo.partyName)+'</td>';
				trStr += '<td>'+StrUtil.strnull(ite.ownerInfo.account)+'</td>';
				
			}else{
				trStr += '<td></td><td></td>';
			}
//		if (ite.statusCd == '1000') {
//			trStr += '<td>有效</td>';
//		} else {
//			trStr += '<td>失效</td>';
//		}
//		trStr += '<td>'+ite.upgradeCondition+'</td>';
//		trStr += '<td>'+""+'</td>';
		if (ite.housingFeeRate != null){
			trStr += '<td>'+ite.housingFeeRate+'</td>';
		}else{
			trStr += '<td></td>';
		}
		
		if (ite.statusName != null){
			trStr += '<td>'+ite.statusName+'</td>';
		}else{
			trStr += '<td></td>';
		}
		trStr +='<td>';
		if (canUpdate){
			trStr +='<a href="editRoomInfo.jsp?roomId='+ite.roomId+'" class="btn defined-role-btn" >修改</a>';
		}
		
//		trStr += '<td><a href="javascript:updatePropertyCompany('+ite.companyId+');" data-toggle="modal" class="btn defined-role-btn" id="btn_edit_role_"'+ite.companyId+'>修改</a>';
//		trStr +='<a href=javascript:removePropertyCompany('+ite.companyId+',"'+ite.companyName+'"); class="btn defined-role-btn"  id="btn_del_role_"'+ite.companyId+'>删除</a>';
		trStr +='<a href="roomInfo.jsp?roomId='+ite.roomId+'" class="btn defined-role-btn" >详情</a></td>';
		trStr += '</tr>';
		$("#listTable>tbody").append(trStr);
	});
}


function removeRoom() {
	var chk_value =[]; 
	$('input[name="check"]:checked').each(function(){ 
		chk_value.push($(this).val()); 
	}); 
	if (chk_value.length == 0) {
		$.sucmodal("提示", "请勾选要删除的房间!",2000);
		return;
	}
	$.altmodal("删除", "您确定要删除所选的房间信息吗?",1000,function(){
		$.post(
				"roomAction!removeRoom.action",
				{inParma: JSON.stringify(chk_value)},
				function(data){
					data = eval("("+data+")");
					if(data.key == "success"){
						$.sucmodal("提交成功", "房间删除成功。",2000,function(){
							location.reload();
		            	});
					}else{
						$.sucmodal("提交失败", "请于管理员联系"+data.msg,2000);
					}
				}
			);
	});
}

//初始化fileinput控件（第一次初始化）
function initFileInput(ctrlName, uploadUrl) {    
    var control = $('#' + ctrlName); 
/**
 * filepreajax
 filepreupload
 */
    control.fileinput({
        language: 'zh', //设置语言
        showPreview: false,
        uploadAsync: false,
        uploadUrl: uploadUrl, //上传的地址
        showCancel:false,
        showUploadedThumbs:false,
        allowedFileExtensions : ['xls', 'xlsx'],//接收的文件后缀
        showUpload: true, //是否显示上传按钮
        showCaption: true,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式          
        	enctype: 'multipart/form-data',
        	validateInitialCount:true,
        	maxFileCount:1
//        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>" 
    }).on('filepreajax', function(event, previewId, index) {
    	//文件上传前动作
        //console.log('File pre ajax triggered');
    }).on('fileuploaded', function(event,data, previewId, index) {
    	//文件上传前动作
        //console.log('File pre ajax triggered');
    }).on('filebatchuploadsuccess', function(event, data,
    		previewId, index) {
    	//var form = data.form, files = data.files, extra = data.extra,
    	response= eval("(" + data.response + ")");
    	if (response.result == "true"){
    		$.sucmodal("提示", response.msg, function() {
				location.href = "roomManager.jsp";
			});
    	}else {
			alert("导入失败"+response.msg);
		}
    	//console.log('File uploaded triggered'); 
    	});;
}

function importUser(){
	/*$.ajax({
        cache: true,
        type: "POST",
        url:"userAction!uploadFile.action",
        data:$('#userInportForm').serialize(),// 你的formid
        async: false,
        error: function(request) {
            alert("Connection error");
        },
        success: function(data) {
            $("#commonLayout_appcreshi").parent().html(data);
        }
    });*/
	$('#roomInportForm').submit();//提交表单
}